﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Configuration;
using Dados_;


namespace Dados_
{
   public class VendaLINQ:IVendaVO
    {
        DigitalTVContextDataContext dataContext;

        public VendaLINQ()
        {
            dataContext = new DigitalTVContextDataContext(ConfigurationManager.ConnectionStrings["DigitalTVCS"].ConnectionString);
        }

        public VendaLINQ(string connectionString)
        {
            dataContext = new DigitalTVContextDataContext(connectionString);
        }

        public VendaLINQ(DigitalTVContextDataContext dataContext)
        {
            this.dataContext = dataContext;
        }

        public Table<VendaVO> GetTable()
        {
            return dataContext.GetTable<VendaVO>();
        }

        #region CRUD

        public void Inserir(VendaVO venda)
        {
            GetTable().InsertOnSubmit(venda);
            SubmitChanges();
        }

       public  void Inserir(IEnumerable<VendaVO> lista)
        {
            GetTable().InsertAllOnSubmit(lista);
        }

        public void Atualizar(VendaVO venda)
        {
            var table = GetTable();
            table.Attach(venda);
            dataContext.Refresh(RefreshMode.KeepCurrentValues, venda);
        }

       public  void Atualizar(IEnumerable<VendaVO> lista)
        {
            GetTable().AttachAll(lista);
            dataContext.Refresh(RefreshMode.KeepCurrentValues, lista); 
        }

        public void Remover(VendaVO venda)
        {
            var table = GetTable();
            var apagar = table.Single(i => i.Equals(venda));
            table.DeleteOnSubmit(apagar);
        }

       public void Remover(IEnumerable<VendaVO> lista)
        {
            var table = GetTable();
            var apagar = table.Where(i => lista.Contains(i));
            table.DeleteAllOnSubmit(apagar);
        }

       public   VendaVO Obter(Func<VendaVO, bool> func)
        {
            return GetTable().Single(func);
        }

      public  IEnumerable<VendaVO> ObterTodos()
        {
            return GetTable();
        }

       
      public   IEnumerable<VendaVO> ObterTodos(Func<VendaVO, bool> func)
        {
            return GetTable().Where(func);
        }

       public  IEnumerable<VendaVO> ObterTodos(int indice)
        {
            return GetTable().Skip(indice);
        }

       public   IEnumerable<VendaVO> ObterTodos(int indice, int quant)
        {
            return GetTable().Skip(indice).Take(quant);
        }

       public  IEnumerable<VendaVO> ObterTodos(int indice, Func<VendaVO, bool> func)
        {
            return GetTable().Where(func).Skip(indice);
        }

      public  IEnumerable<VendaVO> ObterTodos(int indice, int quant, Func<VendaVO, bool> func)
        {
            return GetTable().Where(func).Skip(indice).Take(quant);
        }

      public   List<VendaVO> ObterLista()
        {
            return GetTable().ToList();
        }

        

       public   List<VendaVO> ObterLista(Func<VendaVO, bool> func)
        {
            return GetTable().Where(func).ToList();
        }
      public   List<VendaVO> ObterLista(int indice)
        {
            return GetTable().Skip(indice).ToList();
        }

      public   List<VendaVO> ObterLista(int indice, int quant)
        {
            return GetTable().Skip(indice).Take(quant).ToList();
        }

      public  List<VendaVO> ObterLista(int indice, Func<VendaVO, bool> func)
        {
            return GetTable().Where(func).Skip(indice).ToList();
        }

      
        #endregion

      public  void IniciarTransacao()
        {
            dataContext.Connection.Open();
            dataContext.Transaction = dataContext.Connection.BeginTransaction();
        }

     public   void ComitarTransacao()
        {
            try
            {
                dataContext.SubmitChanges();
                dataContext.Transaction.Commit();
            }
            catch(Exception ex)
            {
                dataContext.Transaction = null;
                if(dataContext.Connection.State == System.Data.ConnectionState.Open)
                    dataContext.Connection.Close();
                throw ex;
            }
        }

     public   void SubmitChanges()
        {
            dataContext.SubmitChanges();
        }






    }

     
    }

